﻿using STMS.DAL;
using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.BLL
{
    public class StoreBLL
    {
        private StoreDAL storeDAL = new StoreDAL();
        /// <summary>
        /// 条件查询仓库信息列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="isDel"></param>
        /// <returns></returns>
        public List<StoreInfo> GetStoreInfos(string keywords, bool isDel)
        {
            int isDeleted = isDel ? 1 : 0;
            return storeDAL.GetStoreInfos(keywords, isDeleted);
        }

        public void AddStore(string storeName, int storeNo, string storeMark)
        {
            int res = storeDAL.AddStore(storeName, storeNo, storeMark);
            if (res == 0)
            {
                MessageBox.Show("添加失败！");
            }
            else
            {
                MessageBox.Show($"{storeName}仓库添加成功！");
            }
        }

        public bool HasRegion(int storeId)
        {
            // 查询仓库分区数量
            int regionCount = storeDAL.GetStoreRegionCount(storeId);
            if (regionCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 逻辑删除仓库函数，将IsDeleted字段设为1
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns> 0代表失败，1代表删除成功，2代表有分区无法删除</returns>
        public int LogicDelStore(int storeId)
        {
           
            int delCode = 0;
            if (HasRegion(storeId))
            {
                delCode = 2;
            }
            else
            {
                bool blDel = storeDAL.Delete(storeId,0,1);
                if (blDel)
                    delCode = 1;
                else
                    delCode = 0;
            }
            return delCode;
        }

        /// <summary>
        /// 逻辑恢复仓库函数，isDeleted设为0
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public bool RecoverStore(int storeId)
        {
            return storeDAL.Delete(storeId, 0, 0);
        }

        /// <summary>
        /// 从数据库移除仓库逻辑
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public bool DelStore(int storeId)
        {
            return storeDAL.Delete(storeId,1,2);
        }
    }
}
